package netudp;
/**
 * UDP传输层
 */

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class DatagramSend {

	public static void main(String[] args) {

		try {
			DatagramSocket ds = new DatagramSocket();
			String s = "你好吗！";//定义字符串
			byte[] tmp=new byte[1024];
			InetAddress addr=InetAddress.getByName("192.168.1.110");//地址
			DatagramPacket dp = new DatagramPacket(tmp,1024,addr,9988);//设置数据包长度，地址，端口
			dp.setData(s.getBytes());
			ds.send(dp);
			//收
			byte[] buf=new byte[1024];
			DatagramPacket inpacket=new DatagramPacket(buf, 1024);
			ds.receive(inpacket);
			System.out.println(new String(inpacket.getData(),0,inpacket.getLength()));
			ds.close();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
